package com.example.controller;

import cn.hutool.json.JSONUtil;
import com.example.common.HttpStatusEnum;
import com.example.entity.ResponseData;
import com.example.pojo.EssayType;
import com.example.pojo.Menu;
import com.example.service.EssayTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文章类型表(EssayType)表控制层
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@RestController
@RequestMapping("essayType")
@Api(tags = "EssayType信息请求类")
public class EssayTypeController {
    /**
     * 服务对象 
     */
    @Resource
    private EssayTypeService essayTypeService;

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param essayType 筛选条件
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping("getPage")
    public ResponseData getPage(Integer currentPage, Integer pageSize, EssayType essayType) {
        if (currentPage == null || pageSize == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayTypeService.getByPage(currentPage, pageSize, essayType);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据ID查询信息")
    @GetMapping("findById")
    public ResponseData findById(Long id) {
        if (id == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(),HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayTypeService.findById(id);
    }

    /**
     * 新增数据
     *
     * @param  essayType 实体
     * @return 新增结果
     */
    @ApiOperation("增加")
    @PostMapping("add")
    public ResponseData add(EssayType essayType) {
        if(essayType == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayTypeService.add(essayType);
    }

    /**
     * 更新数据
     *
     * @param essayType 实体
     * @return 更新结果
     */
    @ApiOperation("更新")
    @PostMapping("update")
    public ResponseData update(EssayType essayType) {
        if(essayType == null || essayType.getId() == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayTypeService.update(essayType);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 
     */
    @ApiOperation("删除")
    @PostMapping("delete")
    public ResponseData delete(Long id) {
        if (id == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayTypeService.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除")
    @PostMapping("deleteBatch")
    public ResponseData deleteBatch(String ids){
        List<Long> idList = JSONUtil.toList(ids, Long.class);
        if (idList == null || idList.size()<=0){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayTypeService.deleteBatch(idList);
    }

    /**
     * 树形表格查询
     * @param essayType 筛选条件
     * @return 查询结果
     */
    @ApiOperation("树形表格查询")
    @GetMapping("getTreeTable")
    public ResponseData getTreeTable(EssayType essayType) {
        if (essayType == null || !StringUtils.hasText(essayType.getParent())){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayTypeService.getTreeTable(essayType);
    }

    /**
     * 树形缩进选项列表查询
     * @param parent
     * @param deleted
     * @return
     */
    @ApiOperation("树形缩进选项列表查询")
    @GetMapping("getIndentOptions")
    public ResponseData getIndentOptions(String parent, Integer deleted){
        if (!StringUtils.hasText(parent)){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayTypeService.getIndentOptions(parent, deleted);
    }

    /**
     * 树形选项列表查询
     * @param parent
     * @param deleted
     * @return
     */
    @ApiOperation("树形选项列表查询")
    @GetMapping("getTreeOptions")
    public ResponseData getTreeOptions(String parent, Integer deleted){
        if (!StringUtils.hasText(parent)){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayTypeService.getTreeOptions(parent, deleted);
    }

    /**
     * 普通选项列表查询
     * @param deleted
     * @return
     */
    @ApiOperation("普通选项列表查询")
    @GetMapping("getAllOptions")
    public ResponseData getAllOptions(Integer deleted){
        return essayTypeService.getAllOptions(deleted);
    }
}

